layui.config({
    base : "/dhjdglxt/modules/util/"//自定义加载的模块
}).use(['form','layer','jquery','layedit','laydate','laypage','table','commUtil','element','treetable'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        laypage = layui.laypage,
        layedit = layui.layedit,
        laydate = layui.laydate,
        $ = layui.jquery;
    var table = layui.table;
    var commUtil = layui.commUtil;
    var element = layui.element;
    var treetable = layui.treetable;//很重要
    
    
    $.ajax({
        type: "GET",
        url:basePath+'/service/jddtgzsz/jddtgzsz/getJksxglList',
        async:false,
        dataType: "json",
        success: function(data){
       	 	if(data.length > 0){
       	 		for(var i=0; i<data.length; i++ ){
       	 			$("#s_jksxglId").append("<option value=\""+data[i].recId+"\">"+data[i].sxname+"</option>")
       	 		}
       	 	}
       	 	form.render("select");
        }
    });

  //监听工具条
    table.on('tool(modelInfo)', function(obj){
        var data = obj.data;
        if(obj.event === 'remove'){
            remove(data)
        }else if(obj.event === 'edit'){
            edit(data)
        }else if(obj.event === 'view'){
        	view(data)
        }else if(obj.event === 'turnOn'){
        	turnOnOrOff(data)
        }else if(obj.event === 'turnOff'){
        	turnOnOrOff(data)
        }
    });


    $(".onQuery").click(function(){
        onQuery();
    })

    
    //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
    /*
    **新增
    */
    $(window).one("resize",function(){
        $(".onAdd").click(function(){
            var index = layui.layer.open({
                title : "新增",
                type : 2,
                skin:'layui-layer-molv',
                content : basePath+"/service/jddtgzsz/jddtgzsz/0?eric=add",
                area: ['990px', '450px'],
                offset: 't',
                success : function(layero, index){
                    commUtil.resizeLayer(index,'450');
                }
            })
//			layui.layer.full(index);
        })
    }).resize();
    
    //查询
    function onQuery(){
        var s_jksxglId = $('#s_jksxglId').val();
        var s_txtype = $('#s_txtype').val();
        //执行重载
        table.reload('modelInfo', {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
            	s_jksxglId: s_jksxglId,
            	s_txtype: s_txtype
            }
        });
    }
    
    //删除
    function remove(obj) {
        layer.confirm("你确定删除数据吗？", {icon: 3, title:'提示' ,offset: '30%'},
            function(index){//确定回调
        	 $.ajax({
                 type: "DELETE",
                 url:basePath+'/service/jddtgzsz/jddtgzsz/'+obj.recId,
                 async:false,
                 dataType: "json",
                 success: function(data){
                	 location.reload();
                 }
             });
                layer.close(index);
            },function (index) {//取消回调
               layer.close(index);
            }
        );
    }
    //编辑
    function edit(obj) {
	    var index = layui.layer.open({
	        title : "编辑",
	        type : 2,
	        skin:'layui-layer-molv',
	        content : basePath+"/service/jddtgzsz/jddtgzsz/"+obj.recId+"?eric=edit",
	        area: ['990px', '450px'],
	        offset: 't',
	        success : function(layero, index){
	            commUtil.resizeLayer(index,'450');
	        }
	    })
    }
    //查看
    function view(obj) {
	    var index = layui.layer.open({
	        title : "查看",
	        type : 2,
	        skin:'layui-layer-molv',
	        content : basePath+"/service/jddtgzsz/jddtgzsz/"+obj.recId+"?eric=view",
	        area: ['990px', '500px'],
	        offset: 't',
	        success : function(layero, index){
	            commUtil.resizeLayer(index,'500');
	        }
	    })
    }
    
    function turnOnOrOff(obj) {
    	$.ajax({
            type: "GET",
            url:basePath+'/service/jddtgzsz/jddtgzsz/turnOnOrOff/'+obj.recId,
            async:false,
            dataType: "json",
            success: function(data){
            	layer.closeAll('loading');
		    	if(data.code=='0'){
		    		layer.msg("操作成功");
		    		location.reload();	
				}else if(data.code=='5'){
					console.log(data + "-----------------");
					layer.msg(data.message);
				} else {
					layer.msg("操作失败");
				}
            }
        });
    }
})
function HTMLDecode(text) {
    var temp = document.createElement("div");
    temp.innerHTML = text;
    var output = temp.innerText || temp.textContent;
    temp = null;
    return output;
} 